package com.ofsoft.cms.api.v1;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.ofsoft.cms.api.ApiBase;
import com.ofsoft.cms.core.annotation.Action;
import com.ofsoft.cms.core.api.ApiMapping;
import com.ofsoft.cms.core.api.RequestMethod;
import com.ofsoft.cms.core.api.check.EmailCheck;
import com.ofsoft.cms.core.api.check.ParamsCheck;
import com.ofsoft.cms.core.api.check.ParamsCheckType;
import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 内容接口
 *
 * @author OF
 * @date 2017年12月14日
 */
@Action(path = "/content")
public class ContentApi extends ApiBase {

    /**
     * 获取内容信息
     */
    @ApiMapping(method = RequestMethod.GET)
    @ParamsCheck({@ParamsCheckType(name = "content_id",isNotNull = true,checkType = EmailCheck.class), @ParamsCheckType(name = "site_id")})
    public void get() {
        try {
           Record record =  Db.findFirst(Db.getSqlPara("cms.content.detail",getParamsMap()));
            rendSuccessJson(record);
        } catch (Exception e) {
            e.printStackTrace();
            rendFailedJson();
        }
    }

    /**
     * 获取内容列表
     */
    @ApiMapping(method = RequestMethod.GET)
    @ParamsCheck(@ParamsCheckType(name = "site_id"))
    public void list() {
        try {
            rendSuccessJson( Db.find(Db.getSqlPara("cms.content.query",getParamsMap())));
        } catch (Exception e) {
            e.printStackTrace();
            rendFailedJson();
        }
    }

    /**
     * 保存内容信息
     */
    @ApiMapping(method = RequestMethod.GET)
    public void save() {
        Map params = getParamsMap();
        List<Record> list = new ArrayList<Record>();
        try {
            //保存用户前端提交的内容
            Record record = new Record();
            record.set("site_id", params.get("site_id"));
            record.set("template_path", params.get("template_path"));
            record.set("form_id", params.get("form_id"));
            record.set("title_name", params.get("title_name"));
            record.set("topic_id", MapUtils.getIntValue(params,"topic_id",0));
            record.set("column_id", "".equals(params.get("column_id"))||params.get("column_id")==null?0:params.get("column_id"));
            record.set("check_status", "1");
            //record.set("create_people", ShiroUtils.getSysUser().getLoginName());
            record.set("clicks", "0");
            record.set("create_time", new Date());
            record.set("status", "1");
            record.set("sub_column_id", params.get("sub_column_id"));
            Db.save("of_cms_content", "content_id",record);
            String contentId = record.getStr("content_id");
            //组装参数
            //获取表单参数 @liuqi
            SqlPara sql = Db
                    .getSqlPara("cms.field.query", params);
            List<Record> parmsList = Db.find(sql);
            //循环表单参数，没有设置值为空字符串 ，避免原版本前端传为空，后面没存就没法修改保存的问题
            for (Record r : parmsList) {
                String key=r.get("field_name");
                String value=params.get(key)==null?"":params.get(key).toString();
                list.add(new Record().set("name",key).set("value",value ).set("content_id", contentId).set("form_id", params.get("form_id")));
            }
            //批量插入
            Db.batchSave("of_cms_content_field",list, list.size());
            rendSuccessJson();

        } catch (Exception e) {
            e.printStackTrace();
            rendFailedJson();
        }
    }
}
